import { Component,OnInit } from '@angular/core';
import { User } from '../user/user';
import { LoginService } from './login.service';

import { Router } from '@angular/router';
import { UserComponent } from '../user/user.component';
import { Global } from 'src/global';
@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit{
  constructor(private loginService: LoginService, private router: Router) { }
  
  user: User = new User();

  ngOnInit(): void {
    // this.login(); //调用
    // this.user = new User();
  //   if(Global.user!=null){
  //   window.sessionStorage.clear(); //如果判断用户已推出，则清空登录信息
    
  //   Global.user = new User();
  //   location.reload();
  //  }

  }

  login(){
    this.loginService.login(this.user).subscribe(
      (data)=>{           
          if(data["status"]=="ok"){
            Global.user = data["user"];
            window.sessionStorage["user"] = JSON.stringify(Global.user)
            // console.log(Global.user);
            // console.log(JSON.stringify(Global.user))
            this.router.navigate(['/index']) //成功后url跳转
          }else{
             alert("ERROR " + data["status"]);
          }
       }
    );
  }
}
